package cn.bone.base.boot.demo1.web;

import cn.bone.base.boot.demo1.bean.Person;
import cn.bone.base.boot.demo1.dao.PersonDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by MD-01 on 2016/9/5.
 */
@RestController
@RequestMapping("/redis")
public class StringRedisController {

    private Logger logger = LoggerFactory.getLogger(StringRedisController.class);

    @Resource(name="redisTemplate")
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private PersonDao personDao;

    @RequestMapping("/set")
    public String setKeyAndValue(String key,String value){
        logger.info("设置:key={},value={}",key,value);
        ValueOperations<String, String> valOpsStr = redisTemplate.opsForValue();
        valOpsStr.set("test:"+key,value);
        return "set ok";
    }

    @RequestMapping("/get")
    public String getValue(String key){
        logger.info("取值:key={}",key);
        ValueOperations<String, String> valOpsStr = redisTemplate.opsForValue();
        String value = valOpsStr.get("test:"+key);
        return value;
    }

    @RequestMapping("/setPerson")
    public String setPerson(Integer id,String name,Integer age){

        logger.info("设置person:id={},name={},age={}",id,name,age);
        Person person = new Person(id,name,age);
        personDao.save(person);
        return "set person ok";
    }

    @RequestMapping("/getPerson")
    public Person getPerson(Integer id){
        logger.info("获取person:id={}",id);
        return personDao.get(id);
    }

}
